<?php
define('DISABLEDEFENSE', true);
define('DISABLEXSSCHECK', true);

require '../../../../class/class_core.php';
C::app()->init();
try{
    $result = C::m('#boan_payment#payment')->verify('alipay',$_GET);
}catch (Exception $e){
    C::m('#boan_payment#payment')->log($_GET['out_trade_no'],2,'alipay',var_export($e,true), 1);
    $result = false;
}

try{
    if($result) {
        $out_trade_no = $_GET['out_trade_no'];
        
        $trade_no = $_GET['trade_no'];
        $trade_status = $_GET['trade_status'];
        
        $order = C::m('#boan_payment#payment')->getOrder($out_trade_no,true);
        if(empty($order)){
            throw new Exception(lang(P_PAYMENT, 'order_err4'));
        }
        if($order['amount'] != $_GET['total_amount']){
            throw new Exception(lang(P_PAYMENT, 'order_err7'));
        }
        if($order['status'] == 1 ){
            throw new Exception(lang(P_PAYMENT, 'order_err6'));
        }
        
        $order_id = $order['order_id'];
        
        $t = strtotime($_GET['gmt_payment']);
        if($_GET['trade_status'] == 'TRADE_FINISHED') {
            C::m('#boan_payment#payment')->updateOrder($out_trade_no,['pay_method'=>'alipay','status'=>3,'return_info'=>serialize($_GET),'pay_time'=>$t]);
        }
        else if ($_GET['trade_status'] == 'TRADE_SUCCESS') {
            $lockid = 'pay_'.$order_id;
            if(!discuz_process::islocked($lockid)){
                C::m('#boan_payment#payment')->updateOrder($out_trade_no,['pay_method'=>'alipay','status'=>1,'return_info'=>serialize($_GET),'pay_time'=>$t,'trade_id'=>$trade_no]);
                C::m('#boan_payment#payment')->log($order_id,2,'alipay',lang(P_PAYMENT, 'order_info1'), 2,$_GET);
                C::m('#boan_payment#payment')->pluginNotify($order_id,'payNotify');
                discuz_process::unlock($lockid);
            }
        }
        echo "success";	
    }else {
        throw new Exception('Validation failed',204);
        echo "fail";
    }
}catch(Exception $e){
    C::m('#boan_payment#payment')->log($_GET['out_trade_no'],2,'alipay',var_export($e,true), 1,$_GET);
    if($e->getCode() == 204){
        exit('fail');
    }
    exit('success');
}


    		  	  		  	  		     	   	       		   		     		       	   	       		   		     		       	   	       		   		     		       	 	   	    		   		     		       	  	   	    		   		     		       	    			    		   		     		       	  	 	     		   		     		       	  		 	    		   		     		       	  				    		   		     		       	 	   	    		   		     		       	  			      		   		     		       	  	   	    		 	      	  		  	  		     	